package com.retail.msg.controller;

import cn.hutool.core.util.ObjectUtil;
import com.retail.core.common.AbstractCommonController;
import com.retail.core.entity.ParamReq;

import com.retail.core.entity.result.PageResult;
import com.retail.core.entity.result.Result;
import com.retail.global.security.LoginUser;
import com.retail.global.security.LoginUserUtil;
import com.retail.global.annon.TraceLog;
import com.retail.msg.entity.request.RetailMsgRequest;
import com.retail.msg.entity.vo.RetailMsgVo;
import com.retail.msg.service.RetailMsgService;
import com.retail.system.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.retail.core.util.CommonConstants.BUSSINESS_ROLE;

/**
* 消息 API
* @author Administrator
*/
@Api(tags = "消息 API")
@RestController
@RequestMapping("/retailMsg")
public class RetailMsgController extends AbstractCommonController<RetailMsgService, RetailMsgRequest, RetailMsgVo> {

    @Autowired
    private RetailMsgService retailMsgService;

    @Autowired
    private AdminService adminService;

    @TraceLog()
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(retailMsgService, id);
    }

    @TraceLog()
    @PostMapping
    @ApiOperation("保存消息")
    public Result<Integer> save(@RequestBody RetailMsgRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.save(retailMsgService, request);
    }

    @TraceLog()
    @PutMapping
    @ApiOperation("更新消息")
    public Result<Integer> update(@RequestBody RetailMsgRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        return super.update(retailMsgService, request);
    }

    @TraceLog()
    @PostMapping("listPage")
    @ApiOperation("消息-查询")
    public Result<List<RetailMsgVo>>listPage(@RequestBody ParamReq req) {
        return super.listPage(retailMsgService, req);
    }

    @TraceLog()
    @PostMapping("countPage")
    @ApiOperation("消息-统计")
    public Result<Integer> countPage(@RequestBody ParamReq req) {
        return super.countPage(retailMsgService, req);
    }

    @TraceLog()
    @PostMapping("page")
    @ApiOperation("消息-分页数据")
    public Result<PageResult> page(@RequestBody ParamReq<RetailMsgRequest> req) {
        RetailMsgRequest entity = req.getEntity();
        Long adminId =  getLoginUserId();
        LoginUser loginUser = LoginUserUtil.getPrincipal();
        Set<Long> roles =  loginUser.getRolePos().stream().map(f -> f.getId()).collect(Collectors.toSet());
        if (roles.size() == 1 && ObjectUtil.contains(roles, BUSSINESS_ROLE)) {
            entity.setSearchOwnerMsg(1);
            entity.setSearchUserId(adminId);
        } else {
            entity.setSearchUserId(adminId);
            entity.setSearchOwnerMsg(0);
        }

        req.setEntity(entity);
        return super.page(retailMsgService, req);
    }

    @TraceLog()
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除消息")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        return super.delete(retailMsgService, id);
    }

    @TraceLog()
    @ApiOperation("批量删除消息")
    @DeleteMapping("{ids}")
    public Result<Integer> batchDelete(List<Long> ids) {
        return super.batchDelete(retailMsgService, ids);
    }

    @ApiModelProperty("推送通知")
    @GetMapping("delayMsg/{id}")
    public Result<Integer> delayMsg(@PathVariable("id") Long id) {
        return retailMsgService.delayMsg(id);
    }


    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }

}
